<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_obat_rusak extends CI_Model {
	var $table = "obat_hilang";

	public function __construct(){
		parent::__construct();
	}
	
	public function get_all(){
		$sql = "SELECT * FROM $this->table oh JOIN obat o ON oh.KdObat = o.KdObat
		ORDER BY KdObatHilang DESC";
		return $sql;
	}

	public function get_kelompok(){
		$sql = 'SELECT * FROM kelompok';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function search($input){
		$filter = $input['filter'];
		$keyword = $input['keyword'];
		if($filter != "" && $keyword != ""){
			if($filter == "by_name"){
				$sql = "
				SELECT * FROM $this->table oh 
				JOIN obat o ON oh.KdObat = o.KdObat
				WHERE NmObat LIKE '%$keyword%'
				ORDER BY KdObatHilang DESC
				";
			}else if($filter == "by_kelompok"){
				$sql = "
				SELECT KdObatHilang, NmObat,Tanggal, Jumlah, oh.Keterangan FROM $this->table oh 
				JOIN obat o ON oh.KdObat = o.KdObat
				JOIN kelompok k ON o.KdKelompok = k.KdKelompok
				WHERE o.KdKelompok = $keyword
				ORDER BY KdObatHilang DESC
				";
			}else if($filter == "by_tanggal"){
				$before = iddate2dbdate($keyword[1]);
				$after = iddate2dbdate($keyword [0]);
				if($before != "" && $after != ""){
					$sql = "
					SELECT * FROM $this->table oh 
					JOIN obat o ON oh.KdObat = o.KdObat
					WHERE Tanggal BETWEEN '$before' AND '$after'
					ORDER BY KdObatHilang DESC
					";
				}else if($before == "" && $after == ""){
					#apakah after dan before kosong
					$sql = "
					SELECT * FROM $this->table oh 
					JOIN obat o ON oh.KdObat = o.KdObat
					ORDER BY KdObatHilang DESC
					";
				}else{
					if($before == ""){
						$sql = "
						SELECT * FROM $this->table oh 
						JOIN obat o ON oh.KdObat = o.KdObat
						WHERE Tanggal > '$after'
						ORDER BY KdObatHilang DESC
						";
					}else if($after == ""){
						$sql = "
						SELECT * FROM $this->table oh 
						JOIN obat o ON oh.KdObat = o.KdObat
						WHERE Tanggal < '$before'
						ORDER BY KdObatHilang DESC
						";
					}
				}
			}
		}else{
			redirect('obat_rusak', 'refresh');
		}

		return $sql;
	}

	public function store($input){
		$data = array(
			'KdObat'		=> $input['obat'],
			'Tanggal'		=> iddate2dbdate($input['tanggal']),
			'Jumlah'		=> $input['jmlh'],
			'Keterangan'	=> $input['ket'],
			'Pengguna'		=> user_id()
			);
		$do_save = $this->db->insert($this->table, $data);
		if($do_save){
			set_flash('success','Data obat rusak/hilang berhasil ditambahkan');
			return true;
		}else{
			set_flash('danger','Data obat rusak/hilang gagal ditambahkan');
			return false;
		}
	}

	public function get_by_id($id){
		$this->db->where('KdObatHilang', $id);
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($input){
		//Memenggil helper
		$this->load->helper('id_date');
		$data = array(
			'KdObat'		=> $input['obat'],
			'Tanggal'		=> iddate2dbdate($input['tanggal']),
			'Jumlah'		=> $input['jmlh'],
			'Keterangan'	=> $input['ket']
			);
		$this->db->where('KdObatHilang', $input['KdObatHilang']);
		$do_update = $this->db->update($this->table, $data);
		if($do_update){
			set_flash('success','Data obat rusak/hilang berhasil diperbaharui');
			return true;
		}else{
			set_flash('danger','Data obat rusak/hilang gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdObatHilang', $id);
		$do_delete = $this->db->delete($this->table);

		if($do_delete){
			set_flash('success','Data obat rusak/hilang berhasil dihapus');
			return true;
		}else{
			set_flash('danger','Data obat rusak/hilang gagal dihapus');
			return false;
		}
	}

	public function export(){
		$this->db->join('obat', 'obat.KdObat = obat_hilang.KdObat', 'left');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($filter, $param1, $param2){
		if($filter != "" && $param1 != ""){
			if($filter == "by_name"){
				$sql = "
				SELECT * FROM $this->table oh 
				JOIN obat o ON oh.KdObat = o.KdObat
				WHERE NmObat LIKE '%$param1%'
				ORDER BY KdObatHilang DESC
				";
			}else if($filter == "by_kelompok"){
				$sql = "
				SELECT KdObatHilang, NmObat,Tanggal, Jumlah, oh.Keterangan FROM $this->table oh 
				JOIN obat o ON oh.KdObat = o.KdObat
				JOIN kelompok k ON o.KdKelompok = k.KdKelompok
				WHERE o.KdKelompok = $param1
				ORDER BY KdObatHilang DESC
				";
			}else if($filter == "by_tanggal"){
				$before = $param1;
				$after = $param2;
				if($before != "null" && $after != "null"){
					$sql = "
					SELECT * FROM $this->table oh 
					JOIN obat o ON oh.KdObat = o.KdObat
					WHERE Tanggal BETWEEN '$before' AND '$after'
					ORDER BY KdObatHilang DESC
					";
				}else if($before == "null" && $after == "null"){
					#apakah after dan before kosong
					$sql = "
					SELECT * FROM $this->table oh 
					JOIN obat o ON oh.KdObat = o.KdObat
					ORDER BY KdObatHilang DESC
					";
				}else{
					if($before == "null"){
						$sql = "
						SELECT * FROM $this->table oh 
						JOIN obat o ON oh.KdObat = o.KdObat
						WHERE Tanggal > '$after'
						ORDER BY KdObatHilang DESC
						";
					}else if($after == "null"){
						$sql = "
						SELECT * FROM $this->table oh 
						JOIN obat o ON oh.KdObat = o.KdObat
						WHERE Tanggal < '$before'
						ORDER BY KdObatHilang DESC
						";
					}
				}
			}
		}else{
			redirect('obat_rusak', 'refresh');
		}

		$query = $this->db->query($sql);

		if($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		}else{
			$result = array();
		}
		return $result;
	}
}

/* End of file m_obat_rusak.php */
/* Location: ./application/models/m_obat_rusak.php */